//
//  JHBookWordViewController.m
//  JuniRengLish_iOS
//
//  Created by revogi on 2019/5/8.
//  Copyright © 2019 revogi. All rights reserved.
//

#import "JHBookWordViewController.h"
#import "JHBookWordTopTableViewCell.h"
#import "JHBookWordBotTableViewCell.h"

#import "JHBookWordDetailViewController.h"

//类型的模型
#import "JHBookTypeModel.h"


#import "JHBookContTypeModel.h"
#import "JHBookContDataModel.h"
//标志符
static NSString * const TopCellID = @"JHBookWordTopTableViewCell";
static NSString * const BotCellID = @"JHBookWordBotTableViewCell";

@interface JHBookWordViewController () <UITableViewDelegate,UITableViewDataSource>
@property (strong, nonatomic) IBOutlet UIView *topHintTabView;
@property (strong, nonatomic) IBOutlet UIButton *topBtn;

@property (strong, nonatomic) IBOutlet NSLayoutConstraint *headerConstraintHeight;
@property (strong, nonatomic) IBOutlet NSLayoutConstraint *btnConstraintTop;
@property (strong, nonatomic) IBOutlet UITableView *TopTableView;
@property (strong, nonatomic) IBOutlet UITableView *contTableView;
@property (weak, nonatomic) IBOutlet UIButton *navRightBtn;
@property (nonatomic,strong) NSMutableArray *TopTypeDataArray;
@property (nonatomic,strong) NSMutableArray *TopDataArray;

@property (nonatomic,strong) NSMutableArray *contDataArray;
@end

@implementation JHBookWordViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    self.TopTypeDataArray=[NSMutableArray array];
    self.TopDataArray=[NSMutableArray array];
    self.contDataArray=[NSMutableArray array];
    [self requestBookTypeData];
    
    self.navRightBtn.hidden = YES;
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(getPlayerStatus:) name:@"GetPlayerStatus" object:nil];
}

- (void)getPlayerStatus:(NSNotification *)noti
{
    NSNumber * number = noti.object;
    self.navRightBtn.hidden = !number.boolValue;
}

- (void)requestBookTypeData
{
    [self.TopDataArray removeAllObjects];
    [self.TopTypeDataArray removeAllObjects];
    NSMutableDictionary *parmes = [NSMutableDictionary dictionary];
    parmes[@"parentid"] = @"0";
    NSString *urlStr = [BaseUrl stringByAppendingString:book_GetBookList];
    [DJHttpTool postWithURL:urlStr params:parmes success:^(id json) {
        self.TopDataArray=[JHBookTypeModel mj_objectArrayWithKeyValuesArray:json[@"data"]];
        for (JHBookTypeModel *model in self.TopDataArray) {
            [self.TopTypeDataArray addObject:model.namech];
            [self.TopTableView reloadData];
        }
        //默认加载数据的底0个
        JHBookTypeModel *model=self.TopDataArray[0];
        NSInteger ID = [model.ID integerValue];
        NSInteger ParendID=[model.ParentId integerValue];
        [self.topBtn setTitle:model.namech forState:UIControlStateNormal];
        [self.topBtn layoutButtonWithImageStyle:ZJButtonImageStyleRight imageTitleToSpace:10];
        //数据进行异或处理
        NSInteger ParentId=ID^ParendID;
        [self requsetContBookData:[NSString stringWithFormat:@"%ld",ParentId]];
        [self.contTableView reloadData];
    } failure:^(NSError *error) {
        
    }];
}

- (void)initUI
{
    if (IS_IPhoneX_All) {
        self.btnConstraintTop.constant=50;
    }else{
        self.btnConstraintTop.constant=20;
    }
    [self.topBtn setTitleColor:JKMainColor forState:UIControlStateNormal];
    self.TopTableView.delegate=self;
    self.TopTableView.dataSource=self;
    [self.TopTableView registerNib:[UINib nibWithNibName:@"JHBookWordTopTableViewCell" bundle:nil] forCellReuseIdentifier:TopCellID];
    self.TopTableView.scrollEnabled=NO;
    self.TopTableView.rowHeight=33;
    
    //底部的TableView
    
    self.contTableView.dataSource=self;
    self.contTableView.delegate=self;
    [self.contTableView registerNib:[UINib nibWithNibName:@"JHBookWordBotTableViewCell" bundle:nil] forCellReuseIdentifier:BotCellID];
    self.contTableView.rowHeight=120;
    
    [self.navRightBtn setImage:[UIImage sd_animatedGIFNamed:@"playing"] forState:0];
}

#pragma mark  --隐藏导航栏
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.navigationController.navigationBarHidden = YES;
}


- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    self.navigationController.navigationBarHidden = NO;
}

#pragma mark--- -  顶部按钮的点击

- (IBAction)TopBtnDidClick:(UIButton *)sender {
    
    sender.selected=!sender.selected;
    
    if (sender.selected) {
        self.topHintTabView.hidden=NO;
    }else{
        self.topHintTabView.hidden=YES;
    }
}

- (IBAction)navRightBtnClick:(id)sender
{
    [self.navigationController pushViewController:[JHDeviceInfoTool sharedManager].currentVC animated:YES];
}


#pragma mark---   TableView的代理方法

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    if (tableView==self.TopTableView) {
          return 1;
    }
    return 1;
  
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (tableView==self.TopTableView) {
        return self.TopDataArray.count;
    }
    return self.contDataArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0.001;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
 
    return 0.001;
}


- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    return nil;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
 
    return nil;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (tableView==self.TopTableView) {
        JHBookWordTopTableViewCell *TopCell=[tableView dequeueReusableCellWithIdentifier:TopCellID forIndexPath:indexPath];
        TopCell.nameLable.text=self.TopTypeDataArray[indexPath.row];
        return TopCell;
    }else{
        JHBookWordBotTableViewCell *BotCell=[tableView dequeueReusableCellWithIdentifier:BotCellID forIndexPath:indexPath];
        JHBookContTypeModel *model=self.contDataArray[indexPath.row];
        BotCell.gardNameLable.text=model.namech;
        if (model.data.count==2) {
            JHBookContDataModel*model1=model.data[0];
            [BotCell.leftBtn setTitle:model1.namech forState:UIControlStateNormal];
            BotCell.leftBtnActionBlock = ^{
                JHBookWordDetailViewController *vc=[[JHBookWordDetailViewController alloc]init];
                vc.model=model1;
                [self.navigationController pushViewController:vc animated:YES];
                
            };
            
            JHBookContDataModel*model2=model.data[0];
            [BotCell.rightBtn setTitle:model2.namech forState:UIControlStateNormal];
            BotCell.rightBtnActionBlock = ^{
                JHBookWordDetailViewController *vc = [[JHBookWordDetailViewController alloc]init];
                vc.model = model2;
                [self.navigationController pushViewController:vc animated:YES];
            };
        }else if (model.data.count==1){
            JHBookContDataModel*model1 = model.data[0];
            [BotCell.leftBtn setTitle:model1.namech forState:UIControlStateNormal];
            BotCell.leftBtnActionBlock = ^{
                JHBookWordDetailViewController *vc = [[JHBookWordDetailViewController alloc]init];
                vc.model = model1;
                [self.navigationController pushViewController:vc animated:YES];
            };
            BotCell.rightBtn.hidden=YES;
        }
        return BotCell;
    }
    return nil;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    if (tableView == self.TopTableView)
    {
        JHBookTypeModel * model = self.TopDataArray[indexPath.row];
        
        NSInteger ID=[model.ID integerValue];
        NSInteger ParendID=[model.ParentId integerValue];
        
        //数据进行异或处理
        NSInteger ParentId=ID^ParendID;
        [self requsetContBookData:[NSString stringWithFormat:@"%ld",ParentId]];
        NSString *str=self.TopTypeDataArray[indexPath.row];
        [self.topBtn setTitle:str forState:UIControlStateNormal];
        [self.topBtn layoutButtonWithImageStyle:ZJButtonImageStyleRight imageTitleToSpace:10];
        self.topHintTabView.hidden=YES;
        self.topBtn.selected=NO;
    }

}

- (void)requsetContBookData:(NSString *)parentid
{
    [self.contDataArray removeAllObjects];
    NSMutableDictionary *parmes=[NSMutableDictionary dictionary];
    parmes[@"parentid"]=parentid;
    NSString *urlStr=[BaseUrl stringByAppendingString:book_GetBookList];
    [DJHttpTool postWithURL:urlStr params:parmes success:^(id json) {
        self.contDataArray=[JHBookContTypeModel  mj_objectArrayWithKeyValuesArray:json[@"data"]];
        [self.contTableView reloadData];
    } failure:^(NSError *error) {
        
    }];
    
    
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
